package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.entity.RecruitInfo;
import com.jyy.prefabricated.entity.TrainingOrganization;
import com.jyy.prefabricated.enums.ApplyStatusEnum;
import com.jyy.prefabricated.enums.UserTypeEnum;
import com.jyy.prefabricated.service.RecruitInfoService;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.RecruitVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 招聘信息
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class RecruitInfoController {

    @Autowired
    private UserUtils userUtils;

    @Autowired
    private RecruitInfoService recruitInfoService;

    /**
     * 新增招聘
     * @param recruitInfo
     * @return
     */
    @PostMapping("/recruits")
    public AjaxResult insert(@RequestBody RecruitInfo recruitInfo){
        log.info("Rest request to insert recruits:{}",recruitInfo);
        TrainingOrganization trainingOrganization = userUtils.getCurrentTrainingOrganization();
        recruitInfo.setTrainingOrganizationId(trainingOrganization.getId());
        int id = recruitInfoService.insert(recruitInfo);
        RecruitInfo result = recruitInfoService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新招聘
     * @param recruitInfo
     * @return
     */
    @PutMapping("/recruits")
    public AjaxResult update(@RequestBody RecruitInfo recruitInfo){
        log.info("Rest request to update recruits:{}",recruitInfo);
        int id = recruitInfoService.update(recruitInfo);
        RecruitInfo result = recruitInfoService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 查询招聘详情
     * @param id
     * @return
     */
    @GetMapping("/recruits/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to apply recruits:{}",id);
        RecruitInfo result = recruitInfoService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除招聘
     * @param id
     * @return
     */
    @DeleteMapping("/recruits/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete recruits:{}",id);
        int row = recruitInfoService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询招聘审核日志
     * @param pageable
     * @return
     */
    @GetMapping("/recruits-log")
    public AjaxResult log(Pageable pageable){
        log.info("Rest request to find recruits apply log");
        SysUser user = userUtils.getCurrentUser();
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<RecruitInfo> recruitInfo = new ArrayList<>();
        if(UserTypeEnum.TRAININGORGANIZATION.getValue().equals(user.getUserType())){
            recruitInfo = recruitInfoService.selectApplyLog(user.getTrainingOrganization().getId());
        }
        PageInfo<RecruitInfo> result = new PageInfo<>(recruitInfo);
        return AjaxResult.success(result);
    }

    /**
     * 通过审核
     * @param id
     * @return
     */
    @PutMapping("/recruits-apply/{id}")
    public AjaxResult apply(@PathVariable Integer id){
        log.info("Rest request to apply recruits:{}",id);
        SysUser user = userUtils.getCurrentUser();
        recruitInfoService.apply(id, user);
        return AjaxResult.success();
    }

    /**
     * 驳回审核
     * @param id
     * @return
     */
    @PutMapping("/recruits-reject/{id}")
    public AjaxResult reject(@PathVariable Integer id){
        log.info("Rest request to reject recruits:{}",id);
        SysUser user = userUtils.getCurrentUser();
        recruitInfoService.reject(id, user);
        return AjaxResult.success();
    }

    /**
     * 分页查询招聘信息
     * @param keyword 关键字
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param applyStatus 审核状态
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/recruits")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String keyword,@RequestParam(required = false, defaultValue = "") String startDate,@RequestParam(required = false, defaultValue = "") String endDate, @RequestParam(required = false) Integer applyStatus, Pageable pageable){
        log.info("Rest request to select recruits by keyword:{},startDate:{},endDate:{},applyStatus:{}",keyword,startDate,endDate,applyStatus);
        SysUser user = userUtils.getCurrentUser();
        Integer trainingOrganizationId = null;
        if(UserTypeEnum.TRAININGORGANIZATION.getValue().equals(user.getUserType())){
            trainingOrganizationId = user.getTrainingOrganization().getId();
        }
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<RecruitInfo> recruitInfo = recruitInfoService.selectByParams(keyword, startDate, endDate,trainingOrganizationId, applyStatus);
        PageInfo<RecruitInfo> result = new PageInfo<>(recruitInfo);
        return AjaxResult.success(result);
    }

    /**
     * 客户端查询招聘信息
     * @param keyword 关键字
     * @param pageable
     * @return
     */
    @GetMapping("/recruits-frontend")
    public AjaxResult selectRecruitVoByName(@RequestParam(required = false, defaultValue = "") String keyword,Pageable pageable){
        log.info("Rest request to select recruits by keyword:{}",keyword);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<RecruitVo> recruitInfo = recruitInfoService.selectRecruitVoByName(keyword);
        PageInfo<RecruitVo> result = new PageInfo<>(recruitInfo);
        return AjaxResult.success(result);
    }

    /**
     * 热门招聘信息查询
     * @param keyword 关键字
     * @return
     */
    @GetMapping("/recruits-hot")
    public AjaxResult selectByHot(@RequestParam(required = false, defaultValue = "") String keyword){
        log.info("Rest request to select recruits-hot");
        List<RecruitVo> result = recruitInfoService.selectByHot();
        return AjaxResult.success(result);
    }

    /**
     * 查询机构招聘信息
     * @param keyword 关键字
     * @param trainingOrganizationId 机构id
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/recruits-frontend-list")
    public AjaxResult selectRecruitVoByName(@RequestParam(required = false, defaultValue = "") String keyword,@RequestParam Integer trainingOrganizationId,Pageable pageable){
        log.info("Rest request to select recruits by keyword:{}",keyword);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<RecruitInfo> recruitInfo = recruitInfoService.selectByParams(keyword,null,null,trainingOrganizationId, ApplyStatusEnum.SUCCESS.getValue());
        PageInfo<RecruitInfo> result = new PageInfo<>(recruitInfo);
        return AjaxResult.success(result);
    }
}
